package com.chinatechstar.generator.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateValidator;
import com.chinatechstar.generator.entity.Generator;
import com.chinatechstar.generator.service.GeneratorService;
import com.chinatechstar.generator.vo.GeneratorFieldVO;
import com.chinatechstar.generator.vo.GeneratorVO;

/**
 * 代码信息的控制层
 * 
 * @版权所有 上海至界信息科技有限公司
 */
@RestController
@RequestMapping("/generator")
public class GeneratorController {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private GeneratorService generatorService;

	/**
	 * 获取用于绘制表单的基础数据
	 * @return
	 */
	@GetMapping(path = "/getFormBasicData")
	public ListResult<Object> getFormBasicData(){
		String json = getBasicFormJsonData();
		Map<String, Object> data = new HashMap<>();
		data.put("basic",json);
		return ResultBuilder.buildListSuccess(data);
	}

	/**
	 * 查询代码信息分页
	 * 
	 * @param generatorVO 代码前端参数
	 * @return
	 */
	@GetMapping(path = "/queryGenerator")
	public ListResult<Object> queryGenerator(GeneratorVO generatorVO) {
		Map<String, Object> data = generatorService.queryGenerator(generatorVO.getCurrentPage(), generatorVO.getPageSize(), generatorVO.getModuleName(),
				generatorVO.getServiceName(), generatorVO.getSorter());
		return ResultBuilder.buildListSuccess(data);
	}

	/**
	 * 根据代码信息ID查询对应的实体字段
	 * 
	 * @param generatorFieldVO 代码信息ID
	 * @return
	 */
	@GetMapping(path = "/queryFieldByGeneratorId")
	public ListResult<Object> queryFieldByGeneratorId(@Validated(UpdateValidator.class) GeneratorFieldVO generatorFieldVO) {
		Map<String, Object> data = generatorService.queryFieldByGeneratorId(generatorFieldVO);
		return ResultBuilder.buildListSuccess(data);
	}

	/**
	 * 新增代码信息
	 * 
	 * @param generator 代码信息对象
	 * @return
	 */
	@PostMapping(path = "/addGenerator")
	public ActionResult addGenerator(@Validated(InsertValidator.class) @RequestBody Generator generator) {
		generatorService.insertGenerator(generator);
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 编辑代码信息
	 * 
	 * @param generator 代码信息对象
	 * @return
	 */
	@PutMapping(path = "/updateGenerator")
	public ActionResult updateGenerator(@Validated(UpdateValidator.class) @RequestBody Generator generator) {
		generatorService.updateGenerator(generator);
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 删除代码信息
	 * 
	 * @param id 代码信息ID
	 * @return
	 */
	@PostMapping(path = "/deleteGenerator")
	public ActionResult deleteGenerator(@RequestParam(name = "id", required = true) Long[] id) {
		generatorService.deleteGenerator(id);
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 根据查询条件导出代码信息
	 * 
	 * @param response 响应对象
	 * @param paramMap 参数Map
	 */
	@PostMapping(path = "/exportGenerator")
	public void exportGenerator(HttpServletResponse response, @RequestParam Map<String, Object> paramMap) {
		try {
			List<String> headList = Arrays.asList("ID", "模块名", "服务名", "包名", "实体名", "表名", "主键名", "创建时间");
			List<LinkedHashMap<String, Object>> dataList = generatorService.queryGeneratorForExcel(paramMap);
			ExcelUtils.exportExcel(headList, dataList, "代码信息管理", response);
		} catch (Exception e) {
			logger.warn(e.toString());
		}
	}

	/**
	 * 生成代码资源
	 * 
	 * @param id 代码信息ID
	 */
	@PostMapping(value = "/generateResource")
	public ResponseEntity<byte[]> generateResource(@RequestParam(name = "id", required = true) Long[] id) {
		ResponseEntity<byte[]> responseEntity = null;
		try {
			byte[] byteArray = generatorService.generateResource(id);
			HttpHeaders responseHeaders = new HttpHeaders();
			responseEntity = new ResponseEntity<>(byteArray, responseHeaders, HttpStatus.OK);
		} catch (Exception e) {
			logger.warn(e.toString());
		}
		return responseEntity;
	}

	/**
	 * 返回表单的基础数据
	 * @return
	 */
	private String getBasicFormJsonData() {
		String json ="{\n" + "  \"fields\": [{\n" + "    \"__config__\": {\n" + "      \"label\": \"单行文本\",\n"
			+ "      \"labelWidth\": null,\n" + "      \"showLabel\": true,\n" + "      \"changeTag\": true,\n"
			+ "      \"tag\": \"el-input\",\n" + "      \"tagIcon\": \"input\",\n" + "      \"required\": true,\n"
			+ "      \"layout\": \"colFormItem\",\n" + "      \"span\": 24,\n" + "      \"document\": \"https://element.eleme.cn/#/zh-CN/component/input\",\n"
			+ "      \"regList\": [],\n" + "      \"formId\": 101,\n" + "      \"renderKey\": \"1011610351044036\"\n"
			+ "    },\n" + "    \"__slot__\": {\n" + "      \"prepend\": \"\",\n" + "      \"append\": \"\"\n"
			+ "    },\n" + "    \"placeholder\": \"请输入单行文本\",\n" + "    \"style\": {\n" + "      \"width\": \"100%\"\n"
			+ "    },\n" + "    \"clearable\": true,\n" + "    \"prefix-icon\": \"\",\n" + "    \"suffix-icon\": \"\",\n"
			+ "    \"maxlength\": null,\n" + "    \"show-word-limit\": false,\n" + "    \"readonly\": false,\n"
			+ "    \"disabled\": false,\n" + "    \"__vModel__\": \"field101\"\n" + "  }, {\n" + "    \"__config__\": {\n"
			+ "      \"label\": \"多行文本\",\n" + "      \"labelWidth\": null,\n" + "      \"showLabel\": true,\n"
			+ "      \"tag\": \"el-input\",\n" + "      \"tagIcon\": \"textarea\",\n" + "      \"required\": true,\n"
			+ "      \"layout\": \"colFormItem\",\n" + "      \"span\": 24,\n" + "      \"regList\": [],\n"
			+ "      \"changeTag\": true,\n" + "      \"document\": \"https://element.eleme.cn/#/zh-CN/component/input\",\n"
			+ "      \"formId\": 102,\n" + "      \"renderKey\": \"1021610351045408\"\n" + "    },\n" + "    \"type\": \"textarea\",\n"
			+ "    \"placeholder\": \"请输入多行文本\",\n" + "    \"autosize\": {\n" + "      \"minRows\": 4,\n"
			+ "      \"maxRows\": 4\n" + "    },\n" + "    \"style\": {\n" + "      \"width\": \"100%\"\n"
			+ "    },\n" + "    \"maxlength\": null,\n" + "    \"show-word-limit\": false,\n" + "    \"readonly\": false,\n"
			+ "    \"disabled\": false,\n" + "    \"__vModel__\": \"field102\"\n" + "  }, {\n" + "    \"__config__\": {\n"
			+ "      \"label\": \"密码\",\n" + "      \"showLabel\": true,\n" + "      \"labelWidth\": null,\n"
			+ "      \"changeTag\": true,\n" + "      \"tag\": \"el-input\",\n" + "      \"tagIcon\": \"password\",\n"
			+ "      \"layout\": \"colFormItem\",\n" + "      \"span\": 24,\n" + "      \"required\": true,\n"
			+ "      \"regList\": [],\n" + "      \"document\": \"https://element.eleme.cn/#/zh-CN/component/input\",\n"
			+ "      \"formId\": 103,\n" + "      \"renderKey\": \"1031610351048598\"\n" + "    },\n" + "    \"__slot__\": {\n"
			+ "      \"prepend\": \"\",\n" + "      \"append\": \"\"\n" + "    },\n" + "    \"placeholder\": \"请输入密码\",\n"
			+ "    \"show-password\": true,\n" + "    \"style\": {\n" + "      \"width\": \"100%\"\n" + "    },\n"
			+ "    \"clearable\": true,\n" + "    \"prefix-icon\": \"\",\n" + "    \"suffix-icon\": \"\",\n"
			+ "    \"maxlength\": null,\n" + "    \"show-word-limit\": false,\n" + "    \"readonly\": false,\n"
			+ "    \"disabled\": false,\n" + "    \"__vModel__\": \"field103\"\n" + "  }, {\n" + "    \"__config__\": {\n"
			+ "      \"label\": \"计数器\",\n" + "      \"showLabel\": true,\n" + "      \"changeTag\": true,\n"
			+ "      \"labelWidth\": null,\n" + "      \"tag\": \"el-input-number\",\n" + "      \"tagIcon\": \"number\",\n"
			+ "      \"span\": 24,\n" + "      \"layout\": \"colFormItem\",\n" + "      \"required\": true,\n"
			+ "      \"regList\": [],\n" + "      \"document\": \"https://element.eleme.cn/#/zh-CN/component/input-number\",\n"
			+ "      \"formId\": 104,\n" + "      \"renderKey\": \"1041610351049606\"\n" + "    },\n" + "    \"placeholder\": \"计数器\",\n"
			+ "    \"step\": 1,\n" + "    \"step-strictly\": false,\n" + "    \"controls-position\": \"\",\n"
			+ "    \"disabled\": false,\n" + "    \"__vModel__\": \"field104\"\n" + "  }, {\n" + "    \"__config__\": {\n"
			+ "      \"label\": \"下拉选择\",\n" + "      \"showLabel\": true,\n" + "      \"labelWidth\": null,\n"
			+ "      \"tag\": \"el-select\",\n" + "      \"tagIcon\": \"select\",\n" + "      \"layout\": \"colFormItem\",\n"
			+ "      \"span\": 24,\n" + "      \"required\": true,\n" + "      \"regList\": [],\n" + "      \"changeTag\": true,\n"
			+ "      \"document\": \"https://element.eleme.cn/#/zh-CN/component/select\",\n" + "      \"formId\": 119,\n"
			+ "      \"renderKey\": \"1191610351227496\"\n" + "    },\n" + "    \"__slot__\": {\n" + "      \"options\": [{\n"
			+ "        \"label\": \"选项一\",\n" + "        \"value\": 1\n" + "      }, {\n" + "        \"label\": \"选项二\",\n"
			+ "        \"value\": 2\n" + "      }]\n" + "    },\n" + "    \"placeholder\": \"请选择下拉选择\",\n"
			+ "    \"style\": {\n" + "      \"width\": \"100%\"\n" + "    },\n" + "    \"clearable\": true,\n"
			+ "    \"disabled\": false,\n" + "    \"filterable\": false,\n" + "    \"multiple\": false,\n"
			+ "    \"__vModel__\": \"field119\"\n" + "  }, {\n" + "    \"__config__\": {\n" + "      \"label\": \"单选框组\",\n"
			+ "      \"labelWidth\": null,\n" + "      \"showLabel\": true,\n" + "      \"tag\": \"el-radio-group\",\n"
			+ "      \"tagIcon\": \"radio\",\n" + "      \"changeTag\": true,\n" + "      \"layout\": \"colFormItem\",\n"
			+ "      \"span\": 24,\n" + "      \"optionType\": \"default\",\n" + "      \"regList\": [],\n"
			+ "      \"required\": true,\n" + "      \"border\": false,\n" + "      \"document\": \"https://element.eleme.cn/#/zh-CN/component/radio\",\n"
			+ "      \"formId\": 121,\n" + "      \"renderKey\": \"1211610351333669\"\n" + "    },\n" + "    \"__slot__\": {\n"
			+ "      \"options\": [{\n" + "        \"label\": \"选项一\",\n" + "        \"value\": 1\n" + "      }, {\n"
			+ "        \"label\": \"选项二\",\n" + "        \"value\": 2\n" + "      }]\n" + "    },\n" + "    \"style\": {},\n"
			+ "    \"size\": \"medium\",\n" + "    \"disabled\": false,\n" + "    \"__vModel__\": \"field121\"\n"
			+ "  }, {\n" + "    \"__config__\": {\n" + "      \"label\": \"多选框组\",\n" + "      \"tag\": \"el-checkbox-group\",\n"
			+ "      \"tagIcon\": \"checkbox\",\n" + "      \"defaultValue\": [],\n" + "      \"span\": 24,\n"
			+ "      \"showLabel\": true,\n" + "      \"labelWidth\": null,\n" + "      \"layout\": \"colFormItem\",\n"
			+ "      \"optionType\": \"default\",\n" + "      \"required\": true,\n" + "      \"regList\": [],\n"
			+ "      \"changeTag\": true,\n" + "      \"border\": false,\n" + "      \"document\": \"https://element.eleme.cn/#/zh-CN/component/checkbox\",\n"
			+ "      \"formId\": 122,\n" + "      \"renderKey\": \"1221610351335278\"\n" + "    },\n" + "    \"__slot__\": {\n"
			+ "      \"options\": [{\n" + "        \"label\": \"选项一\",\n" + "        \"value\": 1\n" + "      }, {\n"
			+ "        \"label\": \"选项二\",\n" + "        \"value\": 2\n" + "      }]\n" + "    },\n" + "    \"style\": {},\n"
			+ "    \"size\": \"medium\",\n" + "    \"disabled\": false,\n" + "    \"__vModel__\": \"field122\"\n"
			+ "  }, {\n" + "    \"__config__\": {\n" + "      \"label\": \"时间选择\",\n" + "      \"tag\": \"el-time-picker\",\n"
			+ "      \"tagIcon\": \"time\",\n" + "      \"defaultValue\": null,\n" + "      \"span\": 24,\n"
			+ "      \"showLabel\": true,\n" + "      \"layout\": \"colFormItem\",\n" + "      \"labelWidth\": null,\n"
			+ "      \"required\": true,\n" + "      \"regList\": [],\n" + "      \"changeTag\": true,\n"
			+ "      \"document\": \"https://element.eleme.cn/#/zh-CN/component/time-picker\",\n" + "      \"formId\": 123,\n"
			+ "      \"renderKey\": \"1231610351343111\"\n" + "    },\n" + "    \"placeholder\": \"请选择时间选择\",\n"
			+ "    \"style\": {\n" + "      \"width\": \"100%\"\n" + "    },\n" + "    \"disabled\": false,\n"
			+ "    \"clearable\": true,\n" + "    \"picker-options\": {\n" + "      \"selectableRange\": \"00:00:00-23:59:59\"\n"
			+ "    },\n" + "    \"format\": \"HH:mm:ss\",\n" + "    \"value-format\": \"HH:mm:ss\",\n" + "    \"__vModel__\": \"field123\"\n"
			+ "  }, {\n" + "    \"__config__\": {\n" + "      \"label\": \"时间范围\",\n" + "      \"tag\": \"el-time-picker\",\n"
			+ "      \"tagIcon\": \"time-range\",\n" + "      \"span\": 24,\n" + "      \"showLabel\": true,\n"
			+ "      \"labelWidth\": null,\n" + "      \"layout\": \"colFormItem\",\n" + "      \"defaultValue\": null,\n"
			+ "      \"required\": true,\n" + "      \"regList\": [],\n" + "      \"changeTag\": true,\n"
			+ "      \"document\": \"https://element.eleme.cn/#/zh-CN/component/time-picker\",\n" + "      \"formId\": 124,\n"
			+ "      \"renderKey\": \"1241610351343792\"\n" + "    },\n" + "    \"style\": {\n" + "      \"width\": \"100%\"\n"
			+ "    },\n" + "    \"disabled\": false,\n" + "    \"clearable\": true,\n" + "    \"is-range\": true,\n"
			+ "    \"range-separator\": \"至\",\n" + "    \"start-placeholder\": \"开始时间\",\n" + "    \"end-placeholder\": \"结束时间\",\n"
			+ "    \"format\": \"HH:mm:ss\",\n" + "    \"value-format\": \"HH:mm:ss\",\n" + "    \"__vModel__\": \"field124\"\n"
			+ "  }, {\n" + "    \"__config__\": {\n" + "      \"label\": \"上传\",\n" + "      \"tag\": \"el-upload\",\n"
			+ "      \"tagIcon\": \"upload\",\n" + "      \"layout\": \"colFormItem\",\n" + "      \"defaultValue\": null,\n"
			+ "      \"showLabel\": true,\n" + "      \"labelWidth\": null,\n" + "      \"required\": true,\n"
			+ "      \"span\": 24,\n" + "      \"showTip\": false,\n" + "      \"buttonText\": \"点击上传\",\n"
			+ "      \"regList\": [],\n" + "      \"changeTag\": true,\n" + "      \"fileSize\": 2,\n" + "      \"sizeUnit\": \"MB\",\n"
			+ "      \"document\": \"https://element.eleme.cn/#/zh-CN/component/upload\",\n" + "      \"formId\": 125,\n"
			+ "      \"renderKey\": \"1251610351350000\"\n" + "    },\n" + "    \"__slot__\": {\n" + "      \"list-type\": true\n"
			+ "    },\n" + "    \"action\": \"https://jsonplaceholder.typicode.com/posts/\",\n" + "    \"disabled\": false,\n"
			+ "    \"accept\": \"\",\n" + "    \"name\": \"file\",\n" + "    \"auto-upload\": true,\n" + "    \"list-type\": \"picture\",\n"
			+ "    \"multiple\": false,\n" + "    \"__vModel__\": \"field125\"\n" + "  }],\n" + "  \"formRef\": \"elForm\",\n"
			+ "  \"formModel\": \"formData\",\n" + "  \"size\": \"medium\",\n" + "  \"labelPosition\": \"left\",\n"
			+ "  \"labelWidth\": 100,\n" + "  \"formRules\": \"rules\",\n" + "  \"gutter\": 15,\n" + "  \"disabled\": false,\n"
			+ "  \"span\": 24,\n" + "  \"formBtns\": true\n" + "}\n";
		return json;
	}

}
